package com.zw.access.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.access.mapper.AccessReturnMapper;
import com.zw.commons.utlis.BeanUtil;
import com.zw.object.access.AccessRegister;
import com.zw.access.mapper.AccessRegisterMapper;
import com.zw.access.service.AccessRegisterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.object.access.AccessReturn;
import com.zw.object.result.Result;
import com.zw.object.vo.AccessRegisterVo;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 出入登记表 服务实现类
 * </p>
 *
 * @author zw
 * @since 2023-03-12
 */
@Service
public class AccessRegisterServiceImpl extends ServiceImpl<AccessRegisterMapper, AccessRegister> implements AccessRegisterService {

    @Autowired
    public AccessReturnMapper accessReturnMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result register(AccessRegister accessRegister) {
        AccessReturn accessReturn = BeanUtil.copyProperties(accessRegister, AccessReturn::new);
        int insert1 = accessReturnMapper.insert(accessReturn);
        int insert = baseMapper.insert(accessRegister);
        return (insert > 0 && insert1 > 0) ? Result.ok():Result.fail();
    }

    @Override
    public Result pageList(AccessRegisterVo vo) {
        Page<AccessRegister> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        LambdaQueryWrapper<AccessRegister> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getName()), AccessRegister::getName, vo.getName());
        wrapper.eq(vo.getType() != null, AccessRegister::getType, vo.getType());
        if (StrUtil.isNotBlank(vo.getStart()) && StrUtil.isNotBlank(vo.getEnd())){
            DateTime a = DateUtil.parse(vo.getStart() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
            DateTime b = DateUtil.parse(vo.getEnd() + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
            wrapper.between(AccessRegister::getCreateTime, a, b);
        }
        wrapper.orderByDesc(AccessRegister::getCreateTime);
        Page<AccessRegister> selectPage = baseMapper.selectPage(page, wrapper);
        return Result.ok(selectPage);
    }


}
